package com.visikard.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.visikard.application.Constant;
import com.visikard.application.UIMessage;
import com.visikard.asynctask.BaseTask;
import com.visikard.asynctask.GetUserProfileTask;
import com.visikard.ui.R;
import com.visikard.ui.activity.MainHomeActivity;
import com.visikard.ui.activity.RedemptionActivity;
import com.visikard.ui.activity.TransactionHistoryActivity;
import com.visikard.ui.activity.RedemptionActivity.RedeemptionActivityEnum;
import com.visikard.ui.activity.TransactionHistoryActivity.ContentFragment;
import com.visikard.utility.BitmapUtils;
import com.visikard.utility.UILImageUtils;

public class MainHomeFragment extends BaseFragment {
	
	private static final long DELAY_LOADING_IMAGE= 1000;
	
	private ImageView imgAvatar, imgLogout;
	private TextView txtMerchantName;
	private Button btnScanCode, btnTypeCode, btnGiftKard, btnCoupon, btnPunchKard;

	private BaseTask mAsyncTask;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.d("Phuoc", "MainHomeActivity----------------------------onCreateView");
		View view = inflater.inflate(R.layout.main_fragment_new, container, false);
		
		//get controls
		imgAvatar = (ImageView)view.findViewById(R.id.img_merchant_avatar);
		txtMerchantName = (TextView)view.findViewById(R.id.txt_merchant_name);
		btnScanCode = (Button)view.findViewById(R.id.btn_scancode);
		btnTypeCode = (Button)view.findViewById(R.id.btn_typecode);
		btnGiftKard = (Button)view.findViewById(R.id.btn_giftkard);
		btnCoupon = (Button)view.findViewById(R.id.btn_coupon);
		btnPunchKard = (Button)view.findViewById(R.id.btn_punchkard);
		imgLogout = (ImageView)view.findViewById(R.id.img_logout);
		
		imgLogout.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				doLogOut();	
			}
		});
		
		//register events
		btnScanCode.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				doBtnScanCodeClicked();
			}
		});
		btnTypeCode.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				doBtnTypeCodeClicked();
			}
		});
		btnGiftKard.setOnClickListener(new View.OnClickListener() {
					
			@Override
			public void onClick(View v) {
				doBtnGiftCardClicked();
			}
		});
		btnCoupon.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				doBtnCouponClicked();
			}
		});
		btnPunchKard.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				doBtnPunchKardClicked();
			}
		});
				
		//initialize UI		
		if (Constant.defaultProfile == null) {
			mAsyncTask = new GetUserProfileTask(mHandler);
			mAsyncTask.execute();
		} else {
			mHandler.sendEmptyMessage(UIMessage.MSG_LOAD_ACCOUNT_INFO);
		}
		
		return view;
	}

	protected void doLogOut() {
		((MainHomeActivity)getActivity()).displayConfirmLogout();
	}

	private void doBtnPunchKardClicked() {
		Intent intent = new Intent(getActivity(), TransactionHistoryActivity.class);
		intent.putExtra(TransactionHistoryActivity.KEY_STARTUP_FRAGMENT, ContentFragment.PUNCHKARD);
		startActivity(intent);
	}

	private void doBtnGiftCardClicked() {
		Intent intent = new Intent(getActivity(), TransactionHistoryActivity.class);
		intent.putExtra(TransactionHistoryActivity.KEY_STARTUP_FRAGMENT, ContentFragment.GIFTKARD);
		startActivity(intent);
	}

	private void doBtnTypeCodeClicked() {
		Intent intent = new Intent(getActivity(), RedemptionActivity.class);
		intent.putExtra(RedemptionActivity.KEY_STARTUP_FRAGMENT, RedeemptionActivityEnum.TYPECODE);
		startActivity(intent);
	}

	private void doBtnScanCodeClicked() {
		Intent intent = new Intent(getActivity(), RedemptionActivity.class);
		intent.putExtra(RedemptionActivity.KEY_STARTUP_FRAGMENT, RedeemptionActivityEnum.SCANCODE);
		startActivity(intent);
	}
	
	private void doBtnCouponClicked() {
		Intent intent = new Intent(getActivity(), TransactionHistoryActivity.class);
		intent.putExtra(TransactionHistoryActivity.KEY_STARTUP_FRAGMENT, ContentFragment.COUPON);
		startActivity(intent);
	}
	
	private void loadAccountInfo() {
		String fullName = Constant.merchantName;
		if(fullName == null || fullName.isEmpty()){
			if (Constant.defaultProfile != null)
				fullName = Constant.defaultProfile.getfName() + " " + Constant.defaultProfile.getlName();	
		}
		txtMerchantName.setText(fullName);
		
//		ImageLoader.getInstance(getActivity()).loadImage(imgAvatar,
//				Constant.defaultProfile.getImageSmall(),
//				Device.getDeviceWidth() / 8, BitmapUtils.CIRCLE, 0, 0,
//				0);
		int logoSize = getMerchantLogoSize();
		UILImageUtils.loadImage(imgAvatar, //image view
				Constant.merchantLogo, //url
				logoSize, //size W of view
				logoSize, //size H of view
				BitmapUtils.CIRCLE, //round all 
				0, // round size
				0, //shadow
				0, //shadow resource 
				0, 0);
		
		//reshow avatar
		new Handler().postDelayed(new Runnable() {
			
			@Override
			public void run() {
				imgAvatar.setVisibility(View.VISIBLE);				
			}
		}, DELAY_LOADING_IMAGE);

	}
	
	private int getMerchantLogoSize(){
		float sizeDp = getResources().getDimension(R.dimen.dimen_70_140);
		float sizePx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, sizeDp, getResources().getDisplayMetrics());
		return (int)sizePx;
	}

	@Override
	public boolean handleMessage(Message msg) {

		switch (msg.what) {
		case UIMessage.MSG_DISMISS_DIALOG:
			dismissProgressDialog();
			mHandler.sendEmptyMessage(UIMessage.MSG_LOAD_ACCOUNT_INFO);
			return true;
		case UIMessage.MSG_LOAD_ACCOUNT_INFO:
			loadAccountInfo();
			return true;
		}
		return super.handleMessage(msg);
	}
	
}
